package movie.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Cinema;
import movie.entity.Film;
import movie.entity.Hall;
import movie.entity.OrderDetail;
import movie.entity.Price;
import movie.service.CinemaScheduleFindAllService;
import movie.service.FilmFindAllService;
import movie.service.OrderDetailService;

@Controller
public class OrderDetailController {
	@Autowired
	private OrderDetailService orderDetailService;
	@Autowired
	private CinemaScheduleFindAllService cinemaScheduleFindAllService;
	@Autowired
	private FilmFindAllService filmFindAllService;
	
	@RequestMapping(value = "/order/movie", method = RequestMethod.GET)
	public String movieGet(Model model,int movieId) {
		model.addAttribute("OrderNumberByMovieId", orderDetailService.findOrderNumberByMovieId(movieId));
		List<OrderDetail> orderDetails=orderDetailService.findPriceByMovieId(movieId);
		for(OrderDetail orderDetail11:orderDetails)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			orderDetail11.setOrderDateString(format1.format(orderDetail11.getOrderDate()));			
		}
		model.addAttribute("priceByMovieId",orderDetails);
		int num0=orderDetailService.findSeatRateByMovieId(movieId);
		List<Cinema> cinemas = cinemaScheduleFindAllService.find(movieId);
		int num1=0;
		for(Cinema cinema:cinemas)
		{
			num1++;
		}
		float rate=(float)num0/(num1*100);
		System.out.println(num1);
		model.addAttribute("rate",rate);
		return "Film/Filmlook";
	}
	
	
	@RequestMapping(value = "/order/cinema/movie", method = RequestMethod.GET)
	public String cinemaGet(Model model,int movieId,HttpServletRequest request) {
	

		
		HttpSession session = request.getSession(false);   //
		
		int cinemaId=(int) session.getAttribute("cinemaId");                                
		session.setAttribute("movieId", movieId);
		
		OrderDetail orderDetail=new OrderDetail();
		Cinema cinema1=new Cinema();
		cinema1.setCinemaId(cinemaId);
		cinema1.setMovieId(movieId);
		orderDetail.setCinema(cinema1);
		model.addAttribute("OrderNumberByMovieIdCinemaId", orderDetailService.findOrderNumberByMovieIdCinemaId(orderDetail));
		List<OrderDetail> orderDetails=orderDetailService.findPriceByCinemaIdMovieId(orderDetail);
		for(OrderDetail orderDetail11:orderDetails)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			orderDetail11.setOrderDateString(format1.format(orderDetail11.getOrderDate()));			
		}
		model.addAttribute("priceByCinemaIdMovieId",orderDetails);
		Cinema cinema = new Cinema();
		cinema.setMovieId(movieId);
		cinema.setCinemaId(cinemaId);
		List<Cinema> cinemas=cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
		for(Cinema cinema11:cinemas)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
			cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
		}
		model.addAttribute("cinemas", cinemas);
		
		int num0=orderDetailService.findOrderNumberByMovieIdCinemaId(orderDetail);
		List<Cinema> filmCinemas=cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
		int num1=0;
		for(Cinema filmcinema:filmCinemas)
		{
			num1++;
		}
		float rate=(float)num0/(num1*100);
		model.addAttribute("rate",rate);
		return "Cinema/cinemafilmlook";
	}
	
	@RequestMapping(value = "/cinemaorderfind/hallname", method = RequestMethod.GET)
	public String findHallGet(String hallName,Model model,HttpServletRequest request)
	{
		HttpSession session = request.getSession(false);   //
		
		int cinemaId=(int) session.getAttribute("cinemaId"); 
		int movieId=(int) session.getAttribute("movieId"); 
		
	
		
		OrderDetail orderDetail=new OrderDetail();
		Hall hall=new Hall();
		hall.setHallName(hallName);
		Cinema cinema=new Cinema();
		cinema.setHall(hall);
		cinema.setCinemaId(cinemaId);
		cinema.setMovieId(movieId);
		orderDetail.setCinema(cinema);
		model.addAttribute("OrderNumberByMovieIdCinemaId", orderDetailService.findOrderNumberByMovieIdCinemaId(orderDetail));
		List<OrderDetail> orderDetails=orderDetailService.findPriceByCinemaIdMovieId(orderDetail);
		for(OrderDetail orderDetail11:orderDetails)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			orderDetail11.setOrderDateString(format1.format(orderDetail11.getOrderDate()));			
		}
		model.addAttribute("priceByCinemaIdMovieId",orderDetails);
		int num0=orderDetailService.findOrderNumberByMovieIdCinemaId(orderDetail);
		List<Cinema> filmCinemas=cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
		int num1=0;
		for(Cinema filmcinema:filmCinemas)
		{
			num1++;
		}
		float rate=(float)num0/(num1*100);
		model.addAttribute("rate",rate);
		if(!hallName.equals(""))
		{
			List<Cinema> cinemas = cinemaScheduleFindAllService.findByMovieIdCinemaIdHallName(cinema);
			for(Cinema cinema11:cinemas)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
				cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
			}
			model.addAttribute("cinemas", cinemas);
			return "Cinema/cinemafilmlook";
		}
		else 
		{
			List<Cinema> cinemas = cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
			for(Cinema cinema11:cinemas)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
				cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
			}
			model.addAttribute("cinemas", cinemas);
			return "Cinema/cinemafilmlook";
		}
	}
	
}
